<?php
// +----------------------------------------------------------------------
// | 数据库管理
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\Db as DbModel;

class Database extends Base {

    private $DbModel;

    public function _initialize() {
        parent::_initialize();
        $this->DbModel = new DbModel;
    }

    /**
     * 列表
     * @access public
     */
    public function index(){
        $this->isAuth('admin.database.index'); 
        //菜单开始
        $pathMaps = [
            ['name'=>'<i class="iconfont icon-home"></i> 数据库表','url'=>'javascript:;']
        ];
        $editMenu = [
            ['name'=>'<i class="iconfont icon-templatedefault"></i> 优化','url'=>url('admin/database/optimizeddb')],
            ['name'=>'<i class="iconfont icon-survey"></i> 修复','url'=>url('admin/database/repairdb')],
        ];
        $this->assign('pathMaps',json_encode($pathMaps));
        $this->assign('editMenu',json_encode($editMenu));
        //菜单结束
        $this->assign('action',false);
        $this->assign('table_list',$this->DbModel->TableLists());
        return $this->fetch(); 
    }

    /**
     * [optimizeddb 数据库优化]
     * @access public
     */
    public function optimizeddb(){
        $this->isAuth('admin.database.optimizeddb'); 
        //菜单开始
        $pathMaps = [
            ['name'=>'<i class="iconfont icon-home"></i> 数据库表','url'=>'javascript:;']
        ];
        $this->assign('pathMaps',json_encode($pathMaps));
        //菜单结束
        $action = input('action/s');
        if($action == 'isPost'){
            $result = $this->DbModel->optimized();
            return json(['code'=>$result['code'],'data'=>['url'=> url('admin/database/index')],'message'=>$result['message']]);
        }else{
            $info = [
                'posturl' => url('admin/database/optimizeddb'),
                'message' => '你确定要优化数据库吗？',
                'backurl' => url('admin/database/index'),
            ];
            $this->assign('info',$info);
            $this->assign('action',true);
            $this->assign('table_list',$this->DbModel->TableLists());
            return $this->fetch('index'); 
        }
    }

    /**
     * [optimizeddb 数据库修复]
     * @access public
     */
    public function repairdb(){
        $this->isAuth('admin.database.repairdb');
        //菜单开始
        $pathMaps = [
            ['name'=>'<i class="iconfont icon-home"></i> 数据库表','url'=>'javascript:;']
        ];
        $this->assign('pathMaps',json_encode($pathMaps));
        //菜单结束
        $action = input('action/s');
        if($action == 'isPost'){
            $result = $this->DbModel->repair();
            return json(['code'=>$result['code'],'data'=>['url'=> url('admin/database/index')],'message'=>$result['message']]);
        }else{  
            $info = [
                'posturl' => url('admin/database/repairdb'),
                'message' => '你确定要修复数据库吗？',
                'backurl' => url('admin/database/index'),
            ];
            $this->assign('info',$info);
            $this->assign('action',true);
            $this->assign('table_list',$this->DbModel->TableLists());
            return $this->fetch('index'); 
        }
    }
}